package com.example.demo.controller;

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        System.out.println(1 << 3);
        Solution solution = new Solution();
        List<String> strings = solution.restoreIpAddresses("25525511135");
        for (String temp :
                strings) {
            System.out.println(temp);
        }
        System.out.println(1);
    }

    List<String> res = new ArrayList<>();

    public List<String> restoreIpAddresses(String s) {
        if (s == null || s.length() == 0) {
            return res;
        }
        LinkedList<String> segment = new LinkedList<>();
        dfs(s, 0, segment);
        return res;
    }

    private void dfs(String s, int start, LinkedList<String> segment) {
        if (start == s.length()) {
            if (segment.size() == 4) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < segment.size(); i++) {
                    sb.append(segment.get(i)).append(".");
                }
                sb.substring(0, sb.length() - 1);
                res.add(sb.toString());
            }
            return;
        }
        if (segment.size() >= 4) {
            return;
        }
        for (int i = start; i < s.length() && i < start + 3; i++) {
            String temp = s.substring(start, i + 1);
            int num = Integer.parseInt(temp);
            if (num >= 0 && num <= 255) {
                segment.addLast(temp);
                dfs(s, i + 1, segment);
                segment.removeLast();
            } else {
                break;
            }
        }


    }


}
